<template>
  <div>
    <el-dialog
      :title="title"
      width="600px"
      :visible.sync="isShow"
      @close="closeEvent"
    >
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option
              v-for="item in conversion.role_id"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="item in conversion.status"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <template>
        <div style="text-align: center">
          <el-button @click="isShow = false">取消</el-button>
          <el-button type="primary" @click="submit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit } from '@/api/userList'
export default {
  props: {
    conversion: {
      type: Object,
      required: () => {}
    }
  },
  data() {
    return {
      mode: '',
      isShow: false,
      form: {
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        phone: '', //	是	string	手机号
        role_id: '', //	是	string	角色 、2 管理员、3 老师、4 学生
        status: '', //	否	string	1(启用)0(禁用)
        remark: '' //	否	string	备注
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        email: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
            message: '请输入正确邮箱',
            trigger: 'change'
          }
        ],
        phone: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正式手机号',
            trigger: 'change'
          }
        ],
        role_id: [{ required: true, message: '必填', trigger: 'change' }],
        status: [{ required: true, message: '必填', trigger: 'change' }],
        remark: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {
    title() {
      return this.mode === '新增' ? '新增学科' : '编辑学科'
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === '新增') {
            await userAdd(this.form)
            this.$message.success('添加成功')
          } else if (this.mode === '编辑') {
            await userEdit(this.form)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          this.$emit('getData')
        }
      })
    },
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        phone: '', //	是	string	手机号
        role_id: '', //	是	string	角色 、2 管理员、3 老师、4 学生
        status: '', //	否	string	1(启用)0(禁用)
        remark: '' //	否	string	备注
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  background-color: #1394fa;
  text-align: center;
  .el-dialog__title {
    color: #fff;
  }
}
</style>
